package com.linkage.jz.bankruptcy.adapter.Vote;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.linkage.jz.bankruptcy.R;
import com.linkage.seapeak.pojo.CrediterUser;
import com.linkage.seapeak.pojo.VoteGroup;
import com.linkage.seapeak.utils.CommonUtils;
import com.seapeak.recyclebundle.BaseHolder;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * 表决人员--带分组的adapter
 */
public class EditVoteMemberGroupAdapter extends BaseRecyclerAdapter<BaseHolder> {

    public static final int TYPE_HEADER = 1000;
    public static final int TYPE_PERSON = 1001;
    public static final int TYPE_LOAD_MORE = 1002;

    private Context context;

    private List<Object> mData;

    public EditVoteMemberGroupAdapter(Context context, List<Object> mData) {
        this.context = context;
        this.mData = mData;
    }

    @Override
    public int getItemViewType(int position) {
        final Object item = mData.get(position);
        return item instanceof CrediterUser ? (((CrediterUser) item).getCreditorid().equals("loadMore")
                ? TYPE_LOAD_MORE : TYPE_PERSON) : TYPE_HEADER;
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_HEADER:
                //header中的箭头默认隐藏，如有需要，item_arrow设置为visible即可
                return new CommentViewHolder(LayoutInflater.from(context).inflate(R.layout.adapter_pro_vote_voters_group, parent, false));
            case TYPE_LOAD_MORE:
                return new CommentLoadHolder(LayoutInflater.from(context).inflate(R.layout.adapter_vote_load_more, parent, false));
            case TYPE_PERSON:
            default:
                return new CommentChildViewHolder(LayoutInflater.from(context).inflate(R.layout.adapter_pro_vote_voters_no_group, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(final BaseHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        final Object item = mData.get(position);
        if (holder instanceof CommentViewHolder) {
            final VoteGroup group = (VoteGroup) item;
            CommentViewHolder viewHolder = ((CommentViewHolder) holder);
            viewHolder.tvName.setText(group.getName() + "（" + group.getMemberCount() + "）");
            viewHolder.itemArrow.setRotation(group.isExpand ? 0 : 180);
            viewHolder.expandView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v, position, holder);
                }
            });
            viewHolder.tvAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v, position, holder);
                }
            });
            viewHolder.btnEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v, position, holder);
                }
            });
            viewHolder.btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v, position, holder);
                }
            });
            if(group.getMemberCount()==0)
            {
                viewHolder.btnDelete.setVisibility(View.VISIBLE);
            }else {
                viewHolder.btnDelete.setVisibility(View.GONE);
            }
        } else if (holder instanceof CommentChildViewHolder) {
            final CrediterUser member = (CrediterUser) item;
            CommentChildViewHolder viewHolder2 = ((CommentChildViewHolder) holder);
            Glide.with(context).load(member.getAvatar())
                    .bitmapTransform(new CropCircleTransformation(context))
                    .placeholder(R.drawable.logo)
                    .into(viewHolder2.avatar);
            viewHolder2.name.setText(member.getCreditorname());
            viewHolder2.sureMoney.setText("确认金额：" + CommonUtils.resetMoney(member.getConfirmedMoney()) + "元");


            viewHolder2.online.setVisibility(member.getOnlineStatus() == 2 ? View.VISIBLE : View.GONE);
//            if (member.getOnlineStatus()==2) {
//                online.setVisibility(View.VISIBLE);
//            } else {
//                online.setVisibility(View.INVISIBLE);
//            }

            if (member.isDeclareAsVote())
                viewHolder2.voteMoney.setText("表决金额：" + CommonUtils.resetMoney(member.getVoteMoney()) + "元 （申报金额）");
            else
                viewHolder2.voteMoney.setText("表决金额：" + CommonUtils.resetMoney(member.getVoteMoney()) + "元");

            viewHolder2.voteCode.setText("参会编号：" + member.getMeetingNumber());
            viewHolder2.voteCode.setVisibility(member.getMeetingNumber() != 0 ? View.VISIBLE : View.GONE);

            if (member.getRole().equals("CREDITOR")) {
                viewHolder2.tipsGreen.setVisibility(member.getKindTax() != 0 ? View.VISIBLE : View.GONE);
                viewHolder2.tipsRed.setVisibility(member.getKindGuarantee() != 0 ? View.VISIBLE : View.GONE);
                viewHolder2.tipsYellow.setVisibility(member.getKindOther() != 0 ? View.VISIBLE : View.GONE);
                viewHolder2.tipsGrey.setVisibility(member.getKindOrdinary() != 0 ? View.VISIBLE : View.GONE);
            } else {
                viewHolder2.tipsGreen.setVisibility(View.GONE);
                viewHolder2.tipsRed.setVisibility(View.GONE);
                viewHolder2.tipsYellow.setVisibility(View.GONE);
                viewHolder2.tipsGrey.setVisibility(View.GONE);
            }

//            viewHolder2.tipsGreen.setVisibility((member.getRole().equals("CONTRIBUTOR") && member.getKindTax() != 0) ? View.VISIBLE : View.GONE);
//            viewHolder2.tipsRed.setVisibility((member.getRole().equals("CONTRIBUTOR") && member.getKindGuarantee() != 0) ? View.VISIBLE : View.GONE);
//            viewHolder2.tipsYellow.setVisibility((member.getRole().equals("CONTRIBUTOR") && member.getKindOther() != 0) ? View.VISIBLE : View.GONE);
//            viewHolder2.tipsGrey.setVisibility((member.getRole().equals("CONTRIBUTOR") && member.getKindOrdinary() != 0) ? View.VISIBLE : View.GONE);

            String roleName = setRoleName(member.getRole());
            if ("".equalsIgnoreCase(roleName)) {
                if (member.getCommitteeChairman()==1) {
                    roleName = "主席";
                }
            }
            viewHolder2.roleName.setText(roleName);
            viewHolder2.roleName.setVisibility(TextUtils.isEmpty(roleName) ? View.GONE : View.VISIBLE);

            viewHolder2.changeBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v, position, holder);
                }
            });
            viewHolder2.btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v, position, holder);
                }
            });
        } else if (holder instanceof CommentLoadHolder) {
            ((CommentLoadHolder) holder).load.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v, position, holder);
                }
            });

        }
    }

    /**
     * 设置的角色名称判断
     *
     * @param role
     * @return
     */
    private String setRoleName(String role) {
        if (role == null)
            return "";
        else if (role.equals("STAFF"))
            return "职工代表";
        else if (role.equals("CONTRIBUTOR"))
            return "出资人";
        else if (role.equals("CREDITOR"))
            return "";
        else
            return "";
    }

    public class CommentViewHolder extends BaseHolder {
        private View itemArrow;
        private View expandView;
        private TextView tvName;
        private View tvAdd;
        private Button btnEdit;
        private Button btnDelete;

        public CommentViewHolder(View view) {
            super(view);
            itemArrow = view.findViewById(R.id.item_arrow);
            expandView = view.findViewById(R.id.item_group_root);
            tvName = view.findViewById(R.id.item_name);
            tvAdd = view.findViewById(R.id.item_add);
            btnEdit = view.findViewById(R.id.btn_group_edit);
            btnDelete = view.findViewById(R.id.btn_group_delete);
        }

    }

    public class CommentLoadHolder extends BaseHolder {

        private TextView load;

        public CommentLoadHolder(View view) {
            super(view);
            load = view.findViewById(R.id.load);
        }
    }
    public class CommentChildViewHolder extends BaseHolder {
        private ImageView avatar;
        private TextView name;
        private TextView roleName;
        private TextView tipsGreen;
        private TextView tipsRed;
        private TextView tipsYellow;
        private TextView tipsGrey;
        private TextView sureMoney;
        private TextView voteMoney;
        private TextView changeBtn;
        private TextView voteCode;
        private TextView online;
        private Button btnDelete;

        public CommentChildViewHolder(View view) {
            super(view);
            avatar = view.findViewById(R.id.voter_avatar);
            online = view.findViewById(R.id.online);
            name = view.findViewById(R.id.voter_name);
            roleName = (TextView) view.findViewById(R.id.role_name);
            tipsGreen = (TextView) view.findViewById(R.id.tips_green);
            tipsRed = (TextView) view.findViewById(R.id.tips_red);
            tipsYellow = (TextView) view.findViewById(R.id.tips_yellow);
            tipsGrey = (TextView) view.findViewById(R.id.tips_grey);
            sureMoney = view.findViewById(R.id.voter_sure_money);
            voteMoney = view.findViewById(R.id.voter_vote_money);
            changeBtn = view.findViewById(R.id.voter_change);
            voteCode = view.findViewById(R.id.voter_code);
            btnDelete = view.findViewById(R.id.btnDelete);
        }
    }

}
